package com.yyhn.guilimall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yyhn.guilimall.product.dao.AttrGroupDao;
import com.yyhn.guilimall.product.entity.AttrEntity;
import com.yyhn.guilimall.product.entity.AttrGroupEntity;
import com.yyhn.guilimall.product.service.AttrService;
import com.yyhn.guilimall.product.vo.AttrGroupWithAttrsVo;
import com.yyhn.guilimall.product.vo.web.SpuItemAttrGroupVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyhn.common.utils.PageUtils;
import com.yyhn.common.utils.Query;

import com.yyhn.guilimall.product.service.AttrGroupService;


@Service("attrGroupService")
@Log4j2
//属性分组表
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //遍历属性分组表里面的所有信息
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //获取前端传过来的数据
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        //从前台传过来的数据不为空,就去分组信息表里面查询
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        //当catelogId等于0查询所有分类
        //new Query<AttrGroupEntity>().getPage(params) 分页信息
        //wrapper 当分类表Id等于0时就便利所属数据
        if(catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else {
            //或者根据Id查找对应商品分类表信息
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

    }

    /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //com.atguigu.gulimall.product.vo
        //1、查询分组信息 根据catelogId查询,分组信息表
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询所有属性
        //把查询的数据保存到AttrGroupWithAttrsVo里面,在把所有信息存到List<AttrEntity> attrs 集合
        //当 这个集合里面不等于空，为 true就不过滤信息 为false就过滤掉信息
        // 在方法AttrGroupWithAttrsVo集合给前端展示
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).filter((item->{
            return item.getAttrs()!=null;
        })).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySkuIs(Long spuId, Long catelogId) {
        //1.查出当前sku对应的所有属性分组对应信息，以及当前分组下的所有属性对应的值
        //1)、
       List<SpuItemAttrGroupVo>vos= baseMapper.getAttrGroupWithAttrsBySkuIs(spuId,catelogId);

        return vos;
    }


//    /**
//     * 根据分类id查出所有的分组以及这些组里面的属性
//     * @param catelogId
//     * @return
//     */
//    @Override
//    public List<AttrGroupWithAttrsVo> getAttrGroupWithByCatelogId(Long catelogId)
//    {
//        //com.atguigu.gulimall.product.vo
//        //1、查询分组信息
//        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
//
//        //2、查询所有属性
//        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
//            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
//            BeanUtils.copyProperties(group,attrsVo);
//            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
//                attrsVo.setAttrs(attrs);
//            return attrsVo;
//        }).collect(Collectors.toList());
//
//        return collect;
//    }

}